<template>
	<view class="container top-scroll">
		<view class="df jsb ac f32 bottom-line pd20">
			<view class="w30 df ac">
				<imageIcon :value="iconCal"></imageIcon>
				<view class="ml20">填报周期</view>
			</view>
			<view class="w70 df jse ac" style="text-align: right">
				<uni-datetime-picker
					type="date"
					v-model="paramsData.dailyDateStr"
					@change="changeDate"
					v-if="reportType === 1"
				>
					<view class="df jse ac">
						<view>{{ paramsData.dailyDateStr }}</view>
						<view class="label ml10 mr0">修改</view>
					</view>
				</uni-datetime-picker>
				<view class="df jse ac" v-if="[2, 3].includes(reportType)">
					<view>{{
						reportType === 2
							? paramsData.dailyDateStr.slice(5, 11) +
							  paramsData.dailyDateStr.slice(16, 21)
							: paramsData.dailyDateStr
					}}</view>
					<view class="label ml10 mr0" @tap="chooseDate">修改</view>
				</view>
			</view>
		</view>
		<view class="pl20 pr20 bottom-line pb20">
			<view
				class="back-c6 border-4 mt20 pl10"
				v-if="
					paramsData.scheduleCount > 0 || paramsData.actionCount > 0
				"
				@tap="goViewSchedule"
			>
				<text class="f28">
					本{{
						paramsData.type === 0
							? '日报'
							: paramsData.type === 1
							? '周报'
							: '月报'
					}}关联了
					<text v-if="paramsData.scheduleCount > 0">
						<text class="active-color">{{
							paramsData.scheduleCount
						}}</text>
						个日程
					</text>
					<text text v-if="paramsData.actionCount > 0">
						<text class="active-color">{{
							paramsData.actionCount
						}}</text>
						个行动措施，
					</text>
					完成度
					<text class="active-color">{{
						paramsData.finishDegree
					}}</text>
				</text>
			</view>
			<view class="df ac pt20 pb20">
				<imageIcon :value="iconDetail" width="34rpx"></imageIcon>
				<view class="ml20 f32">填写内容</view>
			</view>
			<view>
				<view v-for="(item, index) in reportTypeList" :key="index">
					<report-input
						:local-data="item"
						:dataIndex="index + 1"
						:dailyId="paramsData.id"
						@focus="textFocus(index)"
						@input="textInput($event, index)"
						@upload="uploadMedia($event, index)"
						@record="uploadRecord($event, index)"
						@delete="deleteRecord($event, index)"
						@choose="chooseTag($event, index)"
						@tagAdd="addNewTag($event, index)"
						@tagDelete="deleteNewTag($event, index)"
					></report-input>
					<view :class="'scroll-' + index"></view>
				</view>
				<staticHistogram
					:titleIndex="reportTypeList.length + 1"
					:reportType="reportType"
					:staticStart="staticStart"
					:staticEnd="staticEnd"
					@update="orderStaticUpdate"
				></staticHistogram>
				<view class="mt20">
					<view class="b f32">数据情况分析：</view>
					<view class="mt20 input-bg radius pt20 pl20 pr20 pb40 f32">
						<textarea
							placeholder="请填写或语音输入数据分析"
							placeholder-class="f32"
							v-model="paramsData.content"
							class="pct100"
						></textarea>
						<report-record
							@upload="uploadVoice"
							:audioUrl="dataVoiceList"
							@delete="deleteVoice"
							:show-text="false"
						></report-record>
					</view>
				</view>
			</view>
		</view>

		<!--可见成员-->
		<view class="bottom-line pl20 pr20"
			><showUserList
				:icon="iconKeJain"
				title="可见成员"
				@click="addUser(0)"
				:localdata="paramsData.viewerUsers"
			></showUserList
		></view>
		<!--审批人-->
		<view class="bottom-line pl20 pr20"
			><showUserList
				:icon="iconShenPi"
				title="审批人"
				@click="addUser(1)"
				:localdata="paramsData.auditorUsers"
			></showUserList
		></view>

		<view
			class="bottom-button-bd"
			:style="onFocus ? 'position:relative' : ''"
		>
			<button
				class="default-button circle w30 mr20 ml20"
				:disabled="isDisabled"
				@tap="$noMultipleClicks(submitSave, 2)"
			>
				存为草稿
			</button>
			<button class="primary-button linear w70 mr20" @tap="submitReport">
				提交
			</button>
		</view>

		<!-- 选择成员 -->
		<user-list
			ref="userRefs"
			:userList="userData"
			@search="searchUserList"
			@complete="updateUserList"
		></user-list>
		<!--选择月份-->
		<select-month
			ref="selectMonth"
			:value="reportDateValue"
			:after="10"
			@submit="submitChooseDate"
		></select-month>
		<!--选择周-->
		<select-week
			ref="selectWeek"
			:value="reportDateValue"
			@submit="submitChooseDate"
		></select-week>
	</view>
</template>

<script>
const dayjs = require('@/common/dayjs.min.js')
import reportInput from './compoents/reportInput.vue'
import userList from './compoents/userList/userList.vue'
import selectMonth from '@/subPackagesLog/components/select-month/select-month.vue'
import selectWeek from '@/subPackagesLog/components/select-week/select-week.vue'
import reportRecord from '@/subPackagesLog/components/reportRecord/reportRecord.vue'
import staticHistogram from '@/subPackagesLog/pages/workLog/components/staticHistogram.vue'
import imageIcon from '@/subPackagesLog/pages/workLog/components/imageIcon.vue'
import showUserList from '@/subPackagesLog/pages/workLog/components/showUserList.vue'
import { scrollError, getCurrentWeekRange, isPicture } from '@/common/utils.js'
import iconDetail from './icon/tianxie-icon.png'
import iconCal from '@/static/img/icon-cal.png'
import iconKeJain from './icon/kejian-icon.png'
import iconShenPi from './icon/shenpi-icon.png'
export default {
	components: {
		reportInput,
		userList,
		selectMonth,
		selectWeek,
		reportRecord,
		staticHistogram,
		imageIcon,
		showUserList,
	},
	data() {
		return {
			dayjs,
			iconDetail,
			iconCal,
			iconKeJain,
			iconShenPi,
			isPicture,
			reportType: 0,
			typeId: '',
			paramsData: {
				id: '',
				orgId: uni.getStorageSync('orgId'),
				templateId: '',
				type: 0,
				dailyDateStr: '',
				viewerUsers: [],
				auditorUsers: [],
				beginTime: '',
				endTime: '',
				details: [],
				journaStatus: 2,
				content: '',
				voice: [],
				hospitalId: '',
				orderOperate: '',
				orderIncome: '',
				careStaff: '',
				actionCount: '',
				scheduleCount: '',
				finishDegree: '',
			},
			reportTypeList: [],
			tempDetail: {},
			userData: [],
			reportDateValue: [],
			chooseUserType: 0,
			loginUser: {},
			userId: '',
			searchUserName: '',
			allUserList: [],
			screenWidth: 0,
			isDisabled: false,
			noClick: true,
			dataVoiceList: [],
			monthEndDate: '',
			staticStart: '',
			staticEnd: '',
			unloadBack: false,
			onFocus: false,
		}
	},
	onLoad(options) {
		let _that = this
		this.reportType = parseInt(options.reportType) || 1
		this.paramsData.templateId = this.typeId = parseInt(options.typeId)
		this.paramsData.id = options.reportId || ''
		uni.setNavigationBarTitle({
			title:
				this.reportType === 1
					? '写日报'
					: this.reportType === 2
					? '写周报'
					: '写月报',
		})
		this.paramsData.type = this.reportType - 1
		this.loginUser = uni.getStorageSync('loginUser')
		this.userId = uni.getStorageSync('userId')
		this.paramsData.viewerUsers.push({
			type: this.chooseUserType,
			userId: this.loginUser.id,
			userName: this.loginUser.name,
		})
		switch (this.reportType) {
			case 1:
				this.paramsData.dailyDateStr = dayjs().format('YYYY-MM-DD')
				this.staticStart = this.staticEnd = dayjs().format('YYYY-MM-DD')
				break
			case 2:
				this.staticStart = getCurrentWeekRange(
					dayjs()
				).currentWeekStartDay
				this.staticEnd = getCurrentWeekRange(dayjs()).currentWeekLastDay
				this.paramsData.dailyDateStr = (
					this.staticStart +
					'~' +
					this.staticEnd
				).replace(/-/g, '/')
				break
			case 3:
				this.paramsData.dailyDateStr =
					dayjs().format('YYYY') + '/' + dayjs().format('MM') + '月'
				this.staticStart = dayjs().startOf('month').format('YYYY-MM-DD')
				this.staticEnd = dayjs().endOf('month').format('YYYY-MM-DD')
				break
		}
		if (this.paramsData.id && options.staticStart && options.staticEnd) {
			this.staticStart = options.staticStart
			this.staticEnd = options.staticEnd
		}
		this.checkDetail()
	},
	onUnload() {
		console.log(this.unloadBack)
		if (!this.unloadBack) {
			this.autoSave()
		}
	},
	onShow() {
		this.unloadBack = false
		if (this.paramsData.id) {
			this.getReportDetailDailyNumber()
		}
		// this.onShowLoad = true
	},
	methods: {
		checkDetail() {
			let _that = this
			const { orgId, templateId, type } = this.paramsData
			const { staticStart, staticEnd } = this
			this.$request
				.request('carer-platform/journal/workJournal/checkDetail', {
					data: {
						orgId,
						templateId,
						type,
						beginTime: staticStart,
						endTime: staticEnd,
					},
					method: 'post',
				})
				.then((res) => {
					if (res.data && res.data.id) {
						_that.paramsData.id = res.data.id
						_that.getReportDetailDailyNumber()
					} else {
						_that.paramsData.id = ''
						_that.paramsData.actionCount = 0
						_that.paramsData.scheduleCount = 0
						_that.paramsData.finishDegree = 0
					}
					_that.getTemplateDetail(() => {
						if (_that.paramsData.id) {
							_that.getReportDetail()
						}
					})
				})
		},
		getReportDetailDailyNumber() {
			this.$request
				.request('carer-platform/journal/workJournal/detail', {
					data: {
						id: this.paramsData.id,
					},
				})
				.then((res) => {
					if (res.success && res.data && res.data.id) {
						let data = res.data
						this.paramsData.actionCount = data.actionCount
						this.paramsData.scheduleCount = data.scheduleCount
						this.paramsData.finishDegree = data.finishDegree
					}
				})
		},
		goViewSchedule() {
			const { id, endTime, beginTime } = this.paramsData
			uni.navigateTo({
				url:
					'/subPackagesLog/pages/workLog/viewSchedule?&journalId=' +
					id +
					'&endDate=' +
					endTime +
					'&startDate=' +
					beginTime,
			})
		},
		//查询汇报详情
		getReportDetail() {
			let _that = this
			this.$request
				.request('carer-platform/journal/workJournal/detail', {
					data: {
						id: this.paramsData.id,
					},
				})
				.then((res) => {
					if (res.success && res.data && res.data.id) {
						let data = res.data
						if (data.type === 0) {
							_that.paramsData.dailyDateStr = data.beginTime
						}
						if (data.type === 1) {
							_that.paramsData.dailyDateStr =
								data.beginTime.replace(/-/g, '/') +
								'~' +
								data.endTime.replace(/-/g, '/')
						}
						if (data.type === 2) {
							_that.paramsData.dailyDateStr =
								data.beginTime.slice(0, 7).replace(/-/g, '/') +
								'月'
						}
						_that.reportTypeList = (data && data.details) || []
						if (_that.reportTypeList.length) {
							let tempDetails =
								_that.tempDetail && _that.tempDetail.details
							_that.reportTypeList.forEach((item) => {
								let tempInfo = tempDetails.find(
									(res) =>
										res.templateDetailId ===
										item.templateDetailId
								)
								if (tempInfo) {
									_that.$set(
										item,
										'placeholder',
										tempInfo.placeholder
									)
								}
								let mediaUrl = []
								if (item.urls && item.urls.length) {
									item.urls.forEach((src) => {
										mediaUrl.push({
											url: src,
											isVideo: !isPicture(src),
										})
									})
								}
								_that.$set(
									item,
									'oldImgNum',
									item.urls && item.urls.length
										? item.urls.length
										: 0
								)
								_that.$set(item, 'mediaUrl', mediaUrl)
								let voiceList = []
								if (item.voices && item.voices.length) {
									item.voices.forEach((voice) => {
										voiceList.push({
											filePath: voice.url,
											time: voice.duration,
											count: voice.duration,
											voiceText: voice.text,
											tempFile: voice.url,
											playStatus: 0,
										})
									})
								}
								_that.$set(item, 'voice', item.voices)
								_that.$set(item, 'voiceList', voiceList)

								if (
									item.workContentLabelVOList &&
									item.workContentLabelVOList.length
								) {
									item.workContentLabelVOList.forEach(
										(tag) => {
											_that.$set(tag, 'choose', false)
											if (
												item.selectedWorkContentLabelVOList &&
												item
													.selectedWorkContentLabelVOList
													.length
											) {
												let selectIds =
													item.selectedWorkContentLabelVOList.map(
														(res) => {
															return res.id
														}
													)
												if (
													selectIds.includes(
														tag.labelId
													)
												) {
													tag.choose = true
												}
											}
										}
									)
								}
							})
						}
						_that.paramsData.viewerUsers = data.viewerUser || []
						_that.paramsData.auditorUsers = data.auditorUser || []
						_that.paramsData.content = data.content || ''
						_that.dataVoiceList = []
						if (data.voices && data.voices.length) {
							data.voices.forEach((item) => {
								_that.dataVoiceList.push({
									filePath: item.url,
									time: item.duration,
									count: item.duration,
									voiceText: item.text,
									playStatus: 0,
								})
							})
						}
						_that.staticStart = data.beginTime || ''
						_that.staticEnd = data.endTime || ''
					}
				})
		},
		//查询模板内容
		getTemplateDetail(func) {
			let _that = this
			this.$request
				.request('carer-platform/journal/workJournalTemplate/detail', {
					data: {
						id: this.typeId,
					},
				})
				.then((res) => {
					if (res.success) {
						_that.tempDetail = res.data || {}
						_that.reportTypeList =
							(res.data && res.data.details) || []
						if (_that.reportTypeList.length > 0) {
							_that.reportTypeList.forEach((item) => {
								_that.$set(item, 'templateDetailId', item.id)
								_that.$set(item, 'voice', [])
								_that.$set(item, 'voiceList', [])
								_that.$set(item, 'urls', [])
								_that.$set(item, 'placeholder', item.content)
								_that.$set(item, 'oldImgNum', 0)
								_that.$set(item, 'mediaUrl', [])
								if (
									item.workContentLabelVOList &&
									item.workContentLabelVOList.length
								) {
									item.workContentLabelVOList.forEach(
										(tag) => {
											_that.$set(tag, 'choose', false)
										}
									)
								}
								item.content = ''
								item.id = ''
							})
						}
						_that.paramsData.viewerUsers = []
						_that.paramsData.viewerUsers.push({
							type: _that.chooseUserType,
							userId: _that.loginUser.id,
							userName: _that.loginUser.name,
						})
						_that.paramsData.auditorUsers = []
						_that.paramsData.content = ''
						_that.dataVoiceList = []
						func && func()
					}
				})
		},
		//获得焦点
		textFocus(index) {
			const { reportTypeList } = this
			this.onFocus = true
			uni.createSelectorQuery()
				.selectAll('.top-scroll')
				.boundingClientRect((res) => {
					let topHeight =
						res && res.length > 0 ? Math.abs(res[0].top) : 0
					uni.createSelectorQuery()
						.selectAll('.scroll-' + index)
						.boundingClientRect((res) => {
							let columnRefTops = res.map((item) => item.top)
							let refHeight = columnRefTops[0]
							if (topHeight > 0) {
								refHeight = topHeight + refHeight
							}
							let defaultH = 600
							let imgList = reportTypeList[index].urls || []
							if (imgList && imgList.length > 2) {
								defaultH = defaultH + 108
							}
							let voiceList = reportTypeList[index].voice || []
							if (voiceList && voiceList.length) {
								let voiceH =
									Math.ceil(voiceList.length / 4) * 28
								defaultH = defaultH + voiceH
							}
							console.log(refHeight - defaultH)

							uni.pageScrollTo({
								duration: 0,
								scrollTop: refHeight - defaultH,
							})
						})
						.exec()
				})
				.exec()
		},
		//文字输入
		textInput(data, index) {
			this.onFocus = false
			this.reportTypeList[index].content = data
			this.autoSave()
		},
		//图片视频上传
		uploadMedia(data, index) {
			this.reportTypeList[index].urls = data.imageUrl || []
			this.reportTypeList[index].mediaUrl = []
			if (data.imageUrl && data.imageUrl.length) {
				data.imageUrl.forEach((src) => {
					this.reportTypeList[index].mediaUrl.push({
						url: src,
						isVideo: !isPicture(src),
					})
				})
			}
		},
		//删除图片
		// deleteImg(data, index) {
		// 	this.reportTypeList[index].oldImgNum = this.reportTypeList[index].oldImgNum - 1
		// 	this.autoSave()
		// },
		//语音上传
		uploadRecord(data, index) {
			let voiceList = []
			if (data && data.filePath) {
				if (!this.reportTypeList[index].voice) {
					this.$set(this.reportTypeList[index], 'voice', [])
				}
				if (!this.reportTypeList[index].voiceList) {
					this.$set(this.reportTypeList[index], 'voiceList', [])
				}
				this.reportTypeList[index].voice.push({
					url: data.filePath,
					duration: data.time,
					text: data.voiceText,
				})
				this.reportTypeList[index].voiceList.push(data)
				//转换的文字叠加
				this.reportTypeList[index].content = this.reportTypeList[index]
					.content
					? this.reportTypeList[index].content + data.voiceText
					: data.voiceText
				this.autoSave()
			}
		},
		//删除语音
		deleteRecord(data, index) {
			if (data || data === 0) {
				this.reportTypeList[index].voice.splice(data, 1)
				this.reportTypeList[index].voiceList.splice(data, 1)
				this.autoSave()
			}
		},
		//修改日期
		changeDate(event) {
			this.staticStart = event
			this.staticEnd = event
			this.checkDetail()
		},
		//添加成员
		addUser(type) {
			this.searchUserName = ''
			this.chooseUserType = type
			this.getUserData(() => {
				this.$refs.userRefs.init()
			})
		},
		//查询成员信息
		getUserData(func) {
			let _that = this
			this.$request
				.request(
					'carer-platform/journal/workJournalTemplate/userList',
					{
						data: {
							id: this.typeId,
							userType: this.chooseUserType,
							userName: this.searchUserName,
							orgId: this.paramsData.orgId,
						},
					}
				)
				.then((res) => {
					if (res.success) {
						let data = res.data
						if (data) {
							if (_that.chooseUserType === 0) {
								data = data.filter(
									(item) =>
										item.id !== uni.getStorageSync('userId')
								)
							}
							if (
								_that.chooseUserType === 0 &&
								_that.paramsData.viewerUsers.length
							) {
								data.forEach((item) => {
									_that.$set(item, 'showFlag', false)
									let chooseUser =
										_that.paramsData.viewerUsers.find(
											(res) => res.userId === item.id
										)
									if (chooseUser) {
										item.showFlag = true
									}
								})
							}
							if (
								_that.chooseUserType === 1 &&
								_that.paramsData.auditorUsers.length
							) {
								data.forEach((item) => {
									_that.$set(item, 'showFlag', false)
									let chooseUser =
										_that.paramsData.auditorUsers.find(
											(res) => res.userId === item.id
										)
									if (chooseUser) {
										item.showFlag = true
									}
								})
							}
						}
						if (!_that.searchUserName) {
							_that.allUserList =
								JSON.parse(JSON.stringify(data)) || []
						}
						_that.userData = data || []
						func && func()
					}
				})
		},
		//搜索
		searchUserList(data) {
			this.searchUserName = data
			this.getUserData()
		},
		//更新成员
		updateUserList(data) {
			let index = data.index
			if (this.allUserList && this.allUserList.length) {
				let allIndex = this.allUserList.findIndex(
					(res) => res.id === data.id
				)
				this.allUserList[allIndex].showFlag =
					!this.allUserList[allIndex].showFlag
			}
			this.userData[index].showFlag = !this.userData[index].showFlag
			this.$set(this.userData, index, this.userData[index])
			let userList = []
			this.allUserList.forEach((item) => {
				if (item.showFlag) {
					userList.push({
						type: this.chooseUserType,
						userId: item.id,
						userName: item.name,
					})
				}
			})
			if (this.chooseUserType === 1) {
				this.paramsData.auditorUsers = userList
			} else {
				userList.unshift({
					type: this.chooseUserType,
					userId: this.loginUser.id,
					userName: this.loginUser.name,
				})
				this.paramsData.viewerUsers = userList
			}
			this.autoSave()
		},
		//页面滚动
		scrollInput(index) {
			setTimeout(() => {
				scrollError('.scroll-' + index)
			}, 120)
		},
		//选择时间
		chooseDate() {
			if (this.reportType === 2) {
				this.$refs.selectWeek.init()
			}
			if (this.reportType === 3) {
				this.$refs.selectMonth.init()
			}
		},
		//确认时间选择
		submitChooseDate(data) {
			this.reportDateValue = data
			if (this.reportType === 2) {
				this.paramsData.dailyDateStr = data[1].replace(/-/g, '/')
				let dateArr = data[1].split('~')
				this.staticStart = dateArr[0] || ''
				this.staticEnd = dateArr[1] || ''
			}
			if (this.reportType === 3) {
				this.paramsData.dailyDateStr =
					data[0] +
					'/' +
					(data[1] <= 9 ? '0' + data[1] : data[1]) +
					'月'
				this.staticStart = dayjs(data[0] + '-' + data[1])
					.startOf('month')
					.format('YYYY-MM-DD')
				this.staticEnd = dayjs(data[0] + '-' + data[1])
					.endOf('month')
					.format('YYYY-MM-DD')
			}
			this.checkDetail()
		},
		//确认提交
		submitReport() {
			const { reportType, typeId, reportTypeList, dataVoiceList } = this
			if (
				!this.paramsData.auditorUsers ||
				!this.paramsData.auditorUsers.length
			) {
				uni.showToast({
					title: '请选择审批人',
					icon: 'error',
				})
				return false
			}
			if (reportType === 1) {
				this.paramsData.beginTime = this.paramsData.dailyDateStr
			}
			if (reportType === 2) {
				let dateArr = this.paramsData.dailyDateStr.split('~')
				this.paramsData.beginTime = dateArr[0].replace(/\//g, '-')
				this.paramsData.endTime = dateArr[1].replace(/\//g, '-')
			}
			if (reportType === 3) {
				this.paramsData.beginTime =
					this.paramsData.dailyDateStr
						.replace('/', '-')
						.replace('月', '') + '-01'
			}
			if (this.reportTypeList && this.reportTypeList.length) {
				this.reportTypeList.forEach((item) => {
					if (
						item.workContentLabelVOList &&
						item.workContentLabelVOList.length
					) {
						let labelIds = item.workContentLabelVOList
							.filter((res) => res.choose === true)
							.map((tag) => {
								return tag.labelId ? tag.labelId : tag.id
							})
						this.$set(item, 'labelIds', labelIds)
					}
				})
			}
			this.paramsData.details = reportTypeList
			this.paramsData.voice = []
			if (dataVoiceList && dataVoiceList.length) {
				dataVoiceList.forEach((item) => {
					this.paramsData.voice.push({
						url: item.filePath,
						duration: item.count,
						text: item.voiceText,
					})
				})
			}
			this.paramsData.journaStatus = 2
			this.unloadBack = true

			uni.setStorageSync('reportData', this.paramsData)

			uni.navigateTo({
				url: '/subPackagesLog/pages/workLog/reportDetail?type=preview',
			})
		},
		//选择标签
		chooseTag(data, index) {
			this.reportTypeList[index].workContentLabelVOList.forEach(
				(item) => {
					let dataId = data.labelId ? data.labelId : data.id
					let itemId = item.labelId ? item.labelId : item.id
					if (dataId === itemId) {
						item.choose = !item.choose
					}
				}
			)
			this.autoSave()
		},
		//添加自定义标签
		addNewTag(val, index) {
			console.log(val, index)
			let data = {
				id: '',
				labelId: '',
				labelType: 2,
				labelName: val,
				showFlag: 2,
				choose: false,
			}
			let _that = this
			this.$request
				.request('carer-platform/journal/contentLabel/add', {
					method: 'post',
					data: {
						labelType: 2,
						labelName: val,
						templateId: this.reportTypeList[index].templateId,
						templateDetailId:
							this.reportTypeList[index].templateDetailId,
						showFlag: 2,
					},
				})
				.then((res) => {
					if (res.success) {
						data.id = data.labelId = res.data || ''
						_that.reportTypeList[index].workContentLabelVOList.push(
							data
						)
						_that.autoSave()
					}
				})
		},
		//删除自定义标签
		deleteNewTag(data, index) {
			let _that = this
			this.$request
				.request('carer-platform/journal/contentLabel/del', {
					data: {
						id: data.id,
					},
				})
				.then((res) => {
					if (res.success) {
						uni.showToast({
							title: '删除成功~',
							icon: 'success',
						})
						let delIndex = _that.reportTypeList[
							index
						].workContentLabelVOList.findIndex(
							(res) => res.id === data.id
						)
						if (delIndex || delIndex === 0) {
							_that.reportTypeList[
								index
							].workContentLabelVOList.splice(delIndex, 1)
							_that.autoSave()
						}
					}
				})
		},

		//数据分晰语音
		uploadVoice(data) {
			this.paramsData.content = this.paramsData.content + data.voiceText
			this.dataVoiceList.push(data)
			this.autoSave()
		},
		//删除数据分析语音
		deleteVoice(data) {
			this.dataVoiceList.splice(data, 1)
			this.autoSave()
		},

		/**
		 * 自动保存
		 */
		autoSave() {
			this.submitSave(1)
		},

		/**
		 * @param {Number} type
		 * type 1 失去焦点保存，2 手动保存草稿
		 */
		submitSave(type) {
			let _that = this
			const { reportType, typeId, dataVoiceList } = this
			this.paramsData.journaStatus = 1
			if (reportType === 1) {
				this.paramsData.beginTime = this.paramsData.dailyDateStr
			}
			if (reportType === 2) {
				let dateArr = this.paramsData.dailyDateStr.split('~')
				this.paramsData.beginTime = dateArr[0].replace(/\//g, '-')
				this.paramsData.endTime = dateArr[1].replace(/\//g, '-')
			}
			if (reportType === 3) {
				this.paramsData.beginTime =
					this.paramsData.dailyDateStr
						.replace('/', '-')
						.replace('月', '') + '-01'
			}
			if (this.reportTypeList && this.reportTypeList.length) {
				this.reportTypeList.forEach((item) => {
					if (
						item.workContentLabelVOList &&
						item.workContentLabelVOList.length
					) {
						let labelIds = item.workContentLabelVOList
							.filter((res) => res.choose === true)
							.map((tag) => {
								return tag.labelId ? tag.labelId : tag.id
							})
						this.$set(item, 'labelIds', labelIds)
					}
				})
			}
			this.paramsData.details = this.reportTypeList
			this.paramsData.voice = []
			if (dataVoiceList && dataVoiceList.length) {
				dataVoiceList.forEach((item) => {
					this.paramsData.voice.push({
						url: item.filePath,
						duration: item.count,
						text: item.voiceText,
					})
				})
			}
			this.isDisabled = true
			this.$request
				.request('carer-platform/journal/workJournal/save', {
					method: 'post',
					data: this.paramsData,
				})
				.then((res) => {
					if (res.success) {
						if (type === 1) {
							_that.paramsData.id = res.data
							_that.getReportDetail()
							_that.isDisabled = false
						} else {
							uni.showToast({
								title: '操作成功',
								icon: 'success',
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: 1,
								})
							}, 1500)
						}
					} else {
						_that.isDisabled = false
					}
				})
		},
		//统计数据读取
		orderStaticUpdate(data) {
			this.paramsData.orderOperate = data
		},
	},
}
</script>

<style scoped lang="scss">
.container {
	background-color: #ffffff;
	min-height: calc(100vh - 160rpx);
	font-size: 28rpx;
	padding-bottom: 160rpx;
	padding-left: 0 !important;
	padding-right: 0 !important;
	.box-bd {
		border: 2rpx #dddddd solid;
		border-radius: 10rpx;
		padding: 0;
	}
	.nav-tag {
		flex: 1;
		margin-left: 10rpx;
		margin-right: 10rpx;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 32rpx;
	}
	.input-bg {
		background-color: #eeeeee;
		border-radius: 10rpx;
	}
	/deep/.uni-calendar-item__weeks-box .uni-calendar-item--checked {
		background-color: $active-color !important;
	}
	/deep/.uni-datetime-picker--btn {
		background: linear-gradient(90deg, #2e95ff 0%, #94c9ff 100%) !important;
	}
}

.label {
	height: 54rpx;
	line-height: 54rpx;
	background: linear-gradient(155deg, #2d94ff 1%, #b7daff 100%);
	border-radius: 10rpx;
	opacity: 1;
	font-size: 32rpx;
	color: #ffffff;
	text-align: center;
	padding: 0 20rpx;
}
.back-c6 {
	background: $blue-color;
}
.border-4 {
	border-radius: 4rpx;
}
</style>
